package npr.gui;

import processing.core.*;

import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;

public abstract class GUI implements PConstants{

    protected static PApplet parent; 
    
    protected int xpos, ypos, width, height;
   
    protected boolean active= false, hasInput= false, enabled= true, display=true, pressed= false;

    protected PFont font;
    public static int defaultFontSize= 14;
    public static String defaultFont= "Verdana-14.vlw";
    public static int defaultFontColor= 0;
    public static int fontColorDisabled= 100;
    protected int fontColor;
    protected int fontSize;
    

    public static void setParent(PApplet applet){

	parent= applet;
    }

    public GUI(int xpos, int ypos, int width, int height){
       this.xpos= xpos;
	this.ypos= ypos;
	this.width= width;
	this.height= height;
	parent.registerMouseEvent(this);
        parent.registerKeyEvent(this);
	fontColor= defaultFontColor;
    }

    public GUI(int xpos, int ypos, int width, int height, PFont font, int fontSize){

	this.xpos= xpos;
	this.ypos= ypos;
	this.width= width;
	this.height= height;
	parent.registerMouseEvent(this);
        parent.registerKeyEvent(this);
	fontColor= defaultFontColor;
	if(font==null) this.font= parent.loadFont(defaultFont);
	else this.font= font;
	this.fontSize= fontSize;
    }

    
    public boolean isActive(){

	return active;
    }

    
    public boolean hasInput(){

	return hasInput;
    }


    public void enable(){
	enabled= true;
	fontColor= defaultFontColor;
    }


    public void disable(){
	enabled= false;
	fontColor= fontColorDisabled;
    }


    protected boolean inside(int x, int y){
	return(x>=xpos && x<=xpos+width && y>=ypos && y<=ypos+height);
    }


    protected float textXPos(String label){
	float labelPos= 0f;
        for (int i= 0; i<label.length(); i++){
            labelPos+= font.width(label.charAt(i))*fontSize;
        }
	return ((float)width-labelPos)/2.0f;
    }


    protected float textYPos(){

	return ((float)height-font.ascent()*fontSize)/2.0f;
    }

    protected float labelXPos(String label){
        float labelPos= fontSize;
        for (int i= 0; i<label.length(); i++){
            labelPos+= font.width(label.charAt(i))*fontSize;
        }
	return labelPos;
    }

    protected void  mousePressed(int x, int y){
	if(inside(x, y)){ 
	    active= true;
	}
	else{
	    active= false;
	}
    }


    protected void rightClick(int x, int y){}


    protected void mouseDragged(int x, int y){}


    protected void mouseReleased(int x, int y){}


    protected void mouseMoved(int x, int y){}


    protected void keyPressed(char keyChar, int keyCode){

	if(active){
	     if(keyChar==RETURN || keyChar==ENTER){
		 hasInput= true;
	     }
	     else hasInput= false;
	 }
    }


    public void mouseEvent(MouseEvent event) {
      
	int x = event.getX();
	int y = event.getY();

        switch (event.getID()) {
        case MouseEvent.MOUSE_PRESSED:
            if(event.getButton()==MouseEvent.BUTTON1){
		if(enabled) mousePressed(x, y); 
            }
            else if(enabled) rightClick(x, y);
            break;
        case MouseEvent.MOUSE_DRAGGED:
            if(enabled) mouseDragged(x, y);
            break;
        case MouseEvent.MOUSE_RELEASED:
            if(enabled) mouseReleased(x, y);
            break;
        case MouseEvent.MOUSE_MOVED:
            if(enabled) mouseMoved(x, y);
            break;
        default:
            break;
        }
    }


    public void keyEvent(KeyEvent event) {
      
	char keyChar= event.getKeyChar();
	int keyCode= event.getKeyCode();

	
        switch (event.getID()) {
        case KeyEvent.KEY_TYPED:
	    if(enabled) keyPressed(keyChar, keyCode);
            break;
        case KeyEvent.KEY_PRESSED:
	    int modifiersEx = event.getModifiersEx();
	    String modifiersString = KeyEvent.getModifiersExText(modifiersEx);
	    String modifierChar= KeyEvent.getKeyText(keyCode);
	    if(!pressed && modifiersString.compareTo("Ctrl")==0 && modifierChar.compareTo("M")==0){
		
		pressed= true;
		display= !display;
	    }
            if(keyChar==CODED){
		if(enabled) keyPressed(keyChar, keyCode);
            }
            break;
	case KeyEvent.KEY_RELEASED:
	    pressed= false;
	    break;
        default:
            break;
        }
    }
}